//结算
app.controller("payment", ["$scope", "$rootScope", "$stateParams", "$state", "$http", function($scope, $rootScope, $stateParams, $state, $http) {
	$rootScope.page.showfooter = false;
	var str = $stateParams.str
	var num = $stateParams.num
	$rootScope.page.goshow = true
	$rootScope.findshopcarNum()
	$(".paytype .icon").click(function() {
		if($(this).attr("class") == "iconfont icon icon-kongxinweigou") {
			$(this).removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
			$(this).parent().siblings().find(".icon").removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
		}
	})
	$(".declaration .icon").click(function() {
		if($(this).attr("class") == "iconfont icon icon-kongxinweigou") {
			$(this).removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
			$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
		} else {
			$(this).removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
		}
	})
	var channel = localStorage.getItem("channel")
	if(channel == 30002) {
		$scope.payshow = true
	} else {
		$scope.payshow = false
		$(".span_pay").find(".icon").removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
	}
	if(num == 1) {
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Order/show_pay',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id: localStorage.getItem("user_ID"),
				token: localStorage.getItem("token"),
				cart_Id: str
			},
		}).success(function(data) {
			console.log(data)
			$rootScope.page.goshow = false
			if(data["sttaus"] == 1) {
				$(".addressdialog").show();
				$(".addressdialog").html("请勿重复提交订单");
				$(".addressdialog").fadeOut(1200);
				$state.go("main.myOrder")
			} else {
				$scope.payList = data["list"]
				$scope.allprice = "" * 1
				angular.forEach($scope.payList, function(data2) {
					$scope.allprice += data2.price * data2.num
				});
				var order_Id = data["order_Id"]
				var order_pay = data["order_pay"]
				localStorage.setItem("orderId", order_Id)
				localStorage.setItem("orderPay", order_pay)
				$scope.toBuy = function() {
					$(".nowpay").css("pointer-events", "none").css("background", "#a8a8a8");
					for(var k = 0; k < $(".span_pay").length; k++) {
						if($(".span_pay").eq(k).find(".icon").attr("class") == "iconfont icon icon-shixinduigoudaibiankuang") {
							if($(".span_pay").eq(k).find("span").html() == "支付宝支付") {
								var type = 1
							} else {
								var type = 2
							}
						}
					}
					if($(".declaration .icon").attr("class") == "iconfont icon icon-kongxinweigou") {
						$(".addressdialog").show();
						$(".addressdialog").html("请先确认看房声明条款");
						$(".addressdialog").fadeOut(1000);
					} else {
						if(channel != 30002) {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/k_openid',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
								},
							}).success(function(data) {
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								localStorage.setItem("addurl", window.location.href)
								window.location.href = data
							}).error(function(data) {
								alert(data)
							})
						} else {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/Wxpay',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
									cart_Id: str,
									type: type,
									order_pay: order_pay,
									order_Id: order_Id
								},
							}).success(function(data) {
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								if(data["status"] == 1) {
									window.location.href = data["list"]
								}
								if(data["status"] == 0) {
									setTimeout(function() {
										$(".addressdialog").show();
										$(".addressdialog").html(data["message"]);
										$(".addressdialog").fadeOut(1200);
										setTimeout(function() {
											$state.go("main.myOrder")
										}, 1200)
									}, 1000)
								}

								//			window.location.href = data
							})
						}
					}

				}
			}
		})
	}
	if(num == 2) { //路线页面跳转到结算页
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Order/promptly',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id: localStorage.getItem("user_ID"),
				token: localStorage.getItem("token"),
				road_id: angular.fromJson($stateParams.str).road_id,
				combo_Id: angular.fromJson($stateParams.str).combo_Id
			},
		}).success(function(data) {
			$rootScope.page.goshow = false
			if(data["sttaus"] == 1) {
				$(".addressdialog").show();
				$(".addressdialog").html("请勿重复提交订单");
				$(".addressdialog").fadeOut(1200);
				$state.go("main.myOrder")
			} else {
				$scope.payList = data["list"]
				$scope.allprice = "" * 1
				angular.forEach($scope.payList, function(data2) {
					$scope.allprice += data2.price * data2.num
				});
				var order_Id = data["order_Id"]
				var order_pay = data["order_pay"]
				localStorage.setItem("orderId", order_Id)
				localStorage.setItem("orderPay", order_pay)
				$scope.toBuy = function() {
					$(".nowpay").css("pointer-events", "none").css("background", "#a8a8a8");
					for(var k = 0; k < $(".span_pay").length; k++) {
						if($(".span_pay").eq(k).find(".icon").attr("class") == "iconfont icon icon-shixinduigoudaibiankuang") {
							if($(".span_pay").eq(k).find("span").html() == "支付宝支付") {
								var type = 1
							} else {
								var type = 2
							}
						}
					}
					if($(".declaration .icon").attr("class") == "iconfont icon icon-kongxinweigou") {
						$(".addressdialog").show();
						$(".addressdialog").html("请先确认看房声明条款");
						$(".addressdialog").fadeOut(1000);
					} else {
						if(channel != 30002) {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/k_openid',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
								},
							}).success(function(data) {
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								localStorage.setItem("addurl", window.location.href)
								window.location.href = data
							}).error(function(data) {
								alert(data)
							})
						} else {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/Wxpay',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
									type: type,
									order_pay: order_pay,
									order_Id: order_Id
								},
							}).success(function(data) {
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								if(channel != 30002) {
									if(data["status"] == 0) {
										setTimeout(function() {
											$(".addressdialog").show();
											$(".addressdialog").html(data["message"]);
											$(".addressdialog").fadeOut(1200);
											setTimeout(function() {
												$state.go("main.myOrder")
											}, 1200)
										}, 1000)
									} else {
										$http({
											method: 'post',
											url: '' + $rootScope.Location + 'Order/k_openid',
											headers: {
												'Content-Type': 'application/x-www-form-urlencoded'
											},
											data: {
												user_Id: localStorage.getItem("user_ID"),
												token: localStorage.getItem("token"),
											},
										}).success(function(data) {
											localStorage.setItem("addurl", window.location.href)
											window.location.href = data
										}).error(function(data) {
											alert(data)
										})
									}

								} else {
									if(data["status"] == 1) {
										window.location.href = data["list"]
									}
									if(data["status"] == 0) {
										setTimeout(function() {
											$(".addressdialog").show();
											$(".addressdialog").html(data["message"]);
											$(".addressdialog").fadeOut(1200);
											setTimeout(function() {
												$state.go("main.myOrder")
											}, 1200)
										}, 1000)
									}
								}
								//			window.location.href = data
							})
						}
					}
				}
			}
		})
	}
	if(num == 3) { //订单里进入
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Order/orderlist_pay',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id: localStorage.getItem("user_ID"),
				token: localStorage.getItem("token"),
				order_Id: $stateParams.str
			},
		}).success(function(data) {
			$rootScope.page.goshow = false
			if(data["sttaus"] == 1) {
				$(".addressdialog").show();
				$(".addressdialog").html("请勿重复提交订单");
				$(".addressdialog").fadeOut(1200);
				$state.go("main.myOrder")
			} else {
				$scope.payList = data["list"]
				$scope.allprice = "" * 1
				angular.forEach($scope.payList, function(data2) {
					$scope.allprice += data2.price * data2.num
				});
				var order_Id = data["order_Id"]
				var order_pay = data["order_pay"]
				localStorage.setItem("orderId", order_Id)
				localStorage.setItem("orderPay", order_pay)
				$scope.toBuy = function() {
					$(".nowpay").css("pointer-events", "none").css("background", "#a8a8a8");
					for(var k = 0; k < $(".span_pay").length; k++) {
						if($(".span_pay").eq(k).find(".icon").attr("class") == "iconfont icon icon-shixinduigoudaibiankuang") {
							if($(".span_pay").eq(k).find("span").html() == "支付宝支付") {
								var type = 1
							} else {
								var type = 2
							}
						}
					}
					if($(".declaration .icon").attr("class") == "iconfont icon icon-kongxinweigou") {
						$(".addressdialog").show();
						$(".addressdialog").html("请先确认看房声明条款");
						$(".addressdialog").fadeOut(1000);
					} else {
						if(channel != 30002) {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/k_openid',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
								},
							}).success(function(data) {
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								localStorage.setItem("addurl", window.location.href)
								window.location.href = data
							}).error(function(data) {
								alert(data)
							})
						} else {
							$http({
								method: 'post',
								url: '' + $rootScope.Location + 'Order/Wxpay',
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								data: {
									user_Id: localStorage.getItem("user_ID"),
									token: localStorage.getItem("token"),
									type: type,
									order_pay: order_pay,
									order_Id: order_Id
								},
							}).success(function(data) {
								console.log(data)
								$(".nowpay").css("pointer-events", "auto").css("background", "#FE8975");
								$(".addressdialog").show();
								$(".addressdialog").html("订单正在提交中，请稍等！");
								$(".addressdialog").fadeOut(500);
								if(channel != 30002) {
									if(data["status"] == 0) {
										setTimeout(function() {
											$(".addressdialog").show();
											$(".addressdialog").html(data["message"]);
											$(".addressdialog").fadeOut(1200);
											setTimeout(function() {
												$state.go("main.myOrder")
											}, 1200)
										}, 1000)
									} else {
										$http({
											method: 'post',
											url: '' + $rootScope.Location + 'Order/k_openid',
											headers: {
												'Content-Type': 'application/x-www-form-urlencoded'
											},
											data: {
												user_Id: localStorage.getItem("user_ID"),
												token: localStorage.getItem("token"),
											},
										}).success(function(data) {
											localStorage.setItem("addurl", window.location.href)
											window.location.href = data
										}).error(function(data) {
											alert(data)
										})
									}
								} else {
									if(data["status"] == 1) {
										window.location.href = data["list"]
									}
									if(data["status"] == 0) {
										setTimeout(function() {
											$(".addressdialog").show();
											$(".addressdialog").html(data["message"]);
											$(".addressdialog").fadeOut(1200);
											setTimeout(function() {
												$state.go("main.myOrder")
											}, 1200)
										}, 1000)
									}
								}
								//window.location.href = data
							})
						}
					}
				}
			}
		})
	}
	$scope.clause = function() {
		$state.go("main.clause")
	}
}])
app.controller("clause", ["$scope","$rootScope", "$http", function($scope,$rootScope, $http) {
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Login/agreement',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id: localStorage.getItem("user_ID"),
				token: localStorage.getItem("token"),
			},
		}).success(function(data) {
			$scope.name = data["list"]["name"]
			$scope.content = data["list"]["content"]
		})
}])
app.controller("weixinPay", ["$scope", "$state", "$rootScope", "$stateParams", "$http", "$location", "$timeout", function($scope, $state, $rootScope, $stateParams, $http, $location, $timeout) {
	//	location.reload()
	var addurl = window.location.href
	var openid = addurl.split("?")[1].split("=")[1]
	$http({
		method: 'post',
		url: '' + $rootScope.Location + 'Order/WxpayApi',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data: {
			user_Id: localStorage.getItem("user_ID"),
			token: localStorage.getItem("token"),
			openid: openid,
			order_id: localStorage.getItem("orderId"),
			order_pay: localStorage.getItem("orderPay")
		}
	}).success(function(data) {
		if(data["status"] == 0) {
			$(".addressdialog").show();
			$(".addressdialog").html(data["sessage"]);
			$(".addressdialog").fadeOut(1200);
			setTimeout(function() {
				$state.go("main.myOrder")
			}, 1200)
		} else {
			var appId = data["appId"]
			var timeStamp = data["timeStamp"]
			var nonceStr = data["nonceStr"]
			var packAge = data["package"]
			var signType = data["signType"]
			var paySign = data["paySign"]

			function onBridgeReady() {
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": appId, //公众号名称，由商户传入     
						"timeStamp": timeStamp, //时间戳，自1970年以来的秒数     
						"nonceStr": nonceStr, //随机串     
						"package": packAge,
						"signType": signType, //微信签名方式     
						"paySign": paySign //微信签名 
					},
					function(res) {
						//                 alert(res.err_msg);
						if(res.err_msg == "get_brand_wcpay_request:ok") {
							// 只有微信公众账号 wx_pub 支付成功的结果会在这里返回，其他的 wap 支付结果都是在 extra 中对应的 URL 跳转。
							$state.go("main.paySuccess")
						} else if(res.err_msg == "get_brand_wcpay_request:fail") {
							//混合支付退余额
							$state.go("main.payError")
						} else if(res.err_msg == "get_brand_wcpay_request:cancel") {
							//混合支付退余额
							window.location.href = localStorage.getItem("addurl")
						} // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
					}
				);
			}
			if(typeof WeixinJSBridge == "undefined") {
				if(document.addEventListener) {
					document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
				} else if(document.attachEvent) {
					document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
					document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
				}
			} else {
				onBridgeReady();
			}
			return false
		}
	}).error(function(data) {
		console.log(33)
		//$(".error_orderNumber").show();
	})

}])
app.controller("payError", ["$scope", "$state", "$rootScope", "$stateParams", "$http", "$location", "$timeout", function($scope, $state, $rootScope, $stateParams, $http, $location, $timeout) {
	$rootScope.page.showfooter = true;
	$rootScope.page.footer = 'mine';
	$scope.tofail = function() {
		window.location.href = localStorage.getItem("addurl")
	}
}])
app.controller("paySuccess", ["$scope", "$state", "$rootScope", "$stateParams", "$http", "$location", "$timeout", function($scope, $state, $rootScope, $stateParams, $http, $location, $timeout) {
	$rootScope.page.showfooter = true;
	$rootScope.page.footer = 'mine';
}])